#include "client.h"
/* Usage:  */
int main(int argc, char *argv[]){                                  
    ARGS_CHECK(argc,3);
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret=connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    int enterFlag=-1;
    // 登录页面
    while(1){
        showMenu();
        int choice=-1;
        scanf("%d",&choice);
        switch(choice){
        case 0:
            enterFlag=doRegister(sockfd);
            break;
        case 1:
            enterFlag=doLogin(sockfd);
            break;
        default:
            printf("Input error, please re-enter.\n");
            break;
        }
        sleep(2);
        if(enterFlag==0){
            printf("Enter successfully.\n");
            break;
        }
        else{
            system("clear");
            continue;
        }
    }
    // buf接收stdin的输入
    char buf[4096]={0};
    // 将命令解析放入commandPacket
    commandPacket_t commandPacket;
    // 获取命令返回结果
    train_t train;
    fd_set rdset;
    int cmdRes=0;
    while(1){
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset)){
            bzero(buf,sizeof(buf));
            bzero(&commandPacket,sizeof(commandPacket));
            bzero(&train,sizeof(train));
            ret=read(STDIN_FILENO,buf,sizeof(buf));
            // 用户退出
            if(ret==0){
                commandPacket.cmd=EXITCOMMAND;
                send(sockfd,&commandPacket,sizeof(commandPacket.cmd),0);
                printf("Bye\n");
                break;
            }
            buf[strlen(buf)-1]='\0';
            /* printf("buf=%s\n",buf); */
            char *cmd=strtok(buf," ");
            char *arg=strtok(NULL," ");

            // 非法指令直接下一轮
            if(isValidCommand(cmd)==0){
                printf("Invalid Command!\n");
                continue;
            }
            // 设置commandPacket
            setCommandPacket(&commandPacket,cmd,arg);
            /* printf("%d,%d,%s\n",commandPacket.cmd,commandPacket.length,commandPacket.argument); */
            // 发给服务端命令
            send(sockfd,&commandPacket,sizeof(CommandSet)
                 +sizeof(commandPacket.length)+commandPacket.length,0);
            if(commandPacket.cmd==PUTSCOMMAND){
                transFile(sockfd,commandPacket.argument);
                printf("puts %s finish.\n",commandPacket.argument);
            }
            if(commandPacket.cmd==GETSCOMMAND){
                putsFile(sockfd,commandPacket.argument);
                printf("gets %s finish.\n",commandPacket.argument);
            }
        }
        if(FD_ISSET(sockfd,&rdset)){
            recvn(sockfd,&cmdRes,sizeof(cmdRes));
            if(cmdRes==-1){
                bzero(&train,sizeof(train));
                recvn(sockfd,&train.length,sizeof(train.length));
                recvn(sockfd,train.data,train.length);
                printf("%s",train.data);
                fflush(stdout);
            }
            else{
                if(commandPacket.cmd!=CDCOMMAND){
                    bzero(&train,sizeof(train));
                    recvn(sockfd,&train.length,sizeof(train.length));
                    recvn(sockfd,train.data,train.length);
                    printf("%s",train.data);
                    fflush(stdout);

                }
            }

        }
    }

    close(sockfd);
    return 0;
}

